// 创建路由实例 让views文件夹的页面能够在app.vue中展示

// 1.0 导入创建方法
import {createRouter,createWebHashHistory,createWebHistory} from 'vue-router'

// 2.0 导入路由(组件)
import Home from '../views/HomeView.vue'
import Login from '../views/LoginView.vue'
import Register from '../views/RegisterView.vue'

// 3.0 创建路由实例
const router = createRouter({
    // 路由历史模式 history /  hash /#/
    history:createWebHashHistory(),
    // 路由映射列表 
    routes:[
        {
            path:'/',
            component:Home,
            // 路由嵌套
            children:[
                {
                    path:"dashboard",
                    component:()=> import('../views/dashboard/DashBoard.vue')
                },
                {
                    path:"customerlist",
                    component:()=> import('../views/business/CustomerList.vue')
                },
                {
                    path:"orderlist",
                    component:()=> import('../views/business/OrderList.vue')
                },
                {
                    path:"addadvert",
                    component:()=> import('../views/advert/AddAdvert.vue')
                },
                {
                    path:"advertlist",
                    component:()=> import('../views/advert/AdvertList.vue')
                },
                {
                    path:"goodlist",
                    component:()=> import('../views/goods/GoodList.vue')
                },
                {
                    path:"addgood",
                    component:()=> import('../views/goods/AddGood.vue')
                },
                {
                    path:"modifyaccount",
                    component:()=> import('../views/account/ModifyAccount.vue')
                },
                
            ],
            beforeEnter: (to, from) => {
                // reject the navigation
                var token = localStorage.getItem('token')
                if(token){
                    return true
                }else{
                    return { path:'/login'}

                }
              },
        },
        {
            path:'/login',
            component:Login
        },
        {
            path:'/register',
            component:Register
        }
    ]
})

// 4.0 导出路由
export default router